﻿using System;
using System.Windows.Forms;
using FastFood.Business.Entities;
using FastFood.Business.Logic;
using FastFood.Windows.Helpers;

namespace FastFood.Windows.Forms.Procesos
{
    public partial class CwfSemiTerminadoBusqueda : UserControl
    {
        public event GetObjeto CwfSemiTerminadoBusqueda_SeleccionEntidad;

        public CwfSemiTerminadoBusqueda()
        {
            InitializeComponent();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            Buscar();
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            Limpiar();
        }

        private void insumosDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (CwfSemiTerminadoBusqueda_SeleccionEntidad != null && insumosBindingSource.Current != null)
                    CwfSemiTerminadoBusqueda_SeleccionEntidad(insumosBindingSource.Current);
            }
        }

        private void txtNombre_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (insumosBindingSource.Count > 0)
                {
                    insumosDataGridView.Focus();
                    insumosBindingSource.MoveFirst();
                }
            }

            else if (e.KeyCode == Keys.Enter)
            {
                if (insumosBindingSource.Count == 1)
                {
                    CwfSemiTerminadoBusqueda_SeleccionEntidad(insumosBindingSource.Current);
                }
            }
        }

        private void CwfInsumoBusqueda_Load(object sender, EventArgs e)
        {
            txtNombre.Focus();
        }

        private void insumosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (CwfSemiTerminadoBusqueda_SeleccionEntidad != null && insumosBindingSource.Current != null)
                CwfSemiTerminadoBusqueda_SeleccionEntidad(insumosBindingSource.Current);
        }

        private void txtNombre_TextChanged(object sender, EventArgs e)
        {
            Buscar();
        }

        private void Buscar()
        {
            insumosBindingSource.DataSource = InsumosManager.Instancia.ListarSemiPreparados(txtNombre.Text);
        }

        public void Limpiar()
        {
            txtNombre.Text = string.Empty;
            insumosBindingSource.Clear();
            txtNombre.Focus();
        }
    }
}
